package com.ruoyi.app.controller.member.mapper;

import com.ruoyi.app.controller.marketing.entity.MemberCouponSearchVO;
import com.ruoyi.app.controller.marketing.entity.MemberCouponVO;
import com.ruoyi.common.core.dao.BaseMapper;
import com.ruoyi.common.entity.DO.member.MemberCouponDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface IMemberCouponMapper extends BaseMapper<MemberCouponDO> {

    List<MemberCouponVO> searchMyList(MemberCouponSearchVO searchVO);

    @Update("UPDATE member_coupon SET member_coupon_status=1 WHERE member_id=#{memberId} and id=#{couponId}")
    int useCoupon(@Param("memberId") long memberId, @Param("couponId") long couponId);

    @Update("UPDATE member_coupon SET member_coupon_status=0 WHERE member_id=#{memberId} and id=#{couponId}")
    void rollBack(@Param("memberId") long memberId, @Param("couponId") long couponId);

    @Select("SELECT COUNT(1) FROM member_coupon  where activity_id=1 and member_id=#{memberId}")
    int countByRegisterActivity(@Param("memberId") long memberId);

    @Select("SELECT COUNT(1) FROM member_coupon mc LEFT JOIN coupon c ON c.id = mc.coupon_id where mc.member_id=#{memberId} and mc.member_coupon_status=#{status} and mc.is_enable=1 and mc.is_delete=0 and c.is_enable=1 and c.is_delete=0")
    int countByStatus(@Param("memberId") long memberId, @Param("status") Integer status);

    @Select("SELECT COUNT(1) FROM member_coupon mc LEFT JOIN coupon c ON c.id = mc.coupon_id where mc.member_id=#{memberId} and mc.is_enable=1 and mc.is_delete=0 and c.is_enable=1 and c.is_delete=0")
    int countTotal(@Param("memberId") long memberId);

    @Select("SELECT COUNT(1) FROM member_coupon WHERE member_id=#{memberId} and member_coupon_status=0 and is_enable=1 and is_delete=0")
    int getUnUseNum(Long memberId);

    @Select("SELECT COUNT(1) FROM member_coupon WHERE member_id=#{memberId} and coupon_id=#{couponId}")
    int getReceiveNum(@Param("memberId") long memberId, @Param("couponId") long couponId);
}
